<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
Class POPASP_STRING	

	'为了减轻重量，特将部分代码写到了另外一个文件
	'POPASP_REPLACE_CLASS_CODE_POPASP'

	''''''''''''''修改函数
	
	'采用递归，删除str左边出现的字符串find
	function [ltrim](str,find)
		if  left( str, len(find) ) = find Then
			[ltrim] = Me.[ltrim]( mid(str,len(find)+1) , find )
		else
			[ltrim] = str
		End If
	End Function
	
	'采用递归，删除str右边出现的字符串find
	function [rtrim](str,find)
		if  right( str, len(find) ) = find Then
			[rtrim] = Me.[rtrim]( left(str,len(str) - len(find) ) , find )
		else
			[rtrim] = str
		End If
	End Function
	
	'采用递归，删除str两端出现的字符串find
	function [trim](str,find)
		[trim] = Me.rtrim(Me.ltrim(str,find),find)
	End Function
	
	'使用另一个字符串填充字符串为指定长度，两侧填充
	'input: 输入字符串。
	'pad_length: 如果 pad_length 的值是负数，小于或者等于输入字符串的长度，不会发生任何填充。
	'pad_string: 如果填充字符的长度不能被 pad_string 整除，那么 pad_string 可能会被缩短。
	function pad(ByVal input,ByRef pad_length,ByVal pad_string)
		pad = padding( input,pad_length,pad_string,"both" )
	end Function
	
	'使用另一个字符串填充字符串为指定长度，右填充
	'参数同pad
	function rpad(ByVal input,ByRef pad_length,ByVal pad_string)
		rpad = padding( input,pad_length,pad_string,"right" )
	end Function
	
	'使用另一个字符串填充字符串为指定长度，左填充
	'参数同pad
	function lpad(ByVal input,ByRef pad_length,ByVal pad_string)
		lpad = padding( input,pad_length,pad_string,"left" )
	end Function
	
	'返回字符串，此字符串与指定字符串顺序相反。
	Function Reverse( ByRef str )
		reverse = StrReverse(str)
	End Function

	'  重复一个字符串
	' input: 待操作的字符串 
	' multiplier : input 被重复的次数
	' multiplier 必须大于等于 0。如果 multiplier 被设置为 0，函数返回空字符串
	Function Repeat( ByRef input,ByRef multiplier )
		dim i
		if multiplier = 0 then
			Repeat = "" : Exit Function
		elseif multiplier < 0 then
			Repeat = input : Exit Function
		end if
		for i = 1 to multiplier
			Repeat = Repeat & input
		next
	End Function
	
	'server.urlencode的改写
	Function URLEncode(ByVal input)
		URLEncode = Server.URLEncode(input)
	End Function
	
	'对server.urlencode函数的解码
	function URLDecode(ByVal input)
		URLDecode = ""
		Dim sl: sl = 1
		Dim tl: tl = 1
		Dim key: key = "%"
		Dim kl: kl = Len(key)
		sl = InStr(sl, input, key, 1)
		Do While sl>0
		If (tl=1 And sl<>1) Or tl<sl Then
		URLDecode = URLDecode & Mid(input, tl, sl-tl)
		End If
		Dim hh, hi, hl
		Dim a
		Select Case UCase(Mid(input, sl+kl, 1))
		Case "U":'Unicode URLEncode
		a = Mid(input, sl+kl+1, 4)
		URLDecode = URLDecode & ChrW("&H" & a)
		sl = sl + 6
		Case "E":'UTF-8 URLEncode
		hh = Mid(input, sl+kl, 2)
		a = Int("&H" & hh)'ascii码
		If Abs(a)<128 Then
		sl = sl + 3
		URLDecode = URLDecode & Chr(a)
		Else
		hi = Mid(input, sl+3+kl, 2)
		hl = Mid(input, sl+6+kl, 2)
		a = ("&H" & hh And &H0F) * 2 ^12 Or ("&H" & hi And &H3F) * 2 ^ 6 Or ("&H" & hl And &H3F)
		If a<0 Then a = a + 65536
		URLDecode = URLDecode & ChrW(a)
		sl = sl + 9
		End If
		Case Else:'Asc URLEncode
		hh = Mid(input, sl+kl, 2)'高位
		a = Int("&H" & hh)'ascii码
		If Abs(a)<128 Then
		sl = sl + 3
		Else
		hi = Mid(input, sl+3+kl, 2)'低位
		a = Int("&H" & hh & hi)'非ascii码
		sl = sl + 6
		End If
		URLDecode = URLDecode & Chr(a)
		End Select
		tl = sl
		sl = InStr(sl, input, key, 1)
		Loop
		URLDecode = URLDecode & Mid(input, tl)
	End function
	
	
	'正则匹配结果
	Function reg_exec( ByRef input, ByRef pattern, ByRef mode )
		Call setReg( pattern,mode )	
		set reg_exec = POP_MVC.reg.Execute( input )
	End Function	
	
	'正则匹配
	Function reg_test( ByRef input, ByRef pattern, ByRef mode )
		Call setReg( pattern,mode )	
		reg_test = POP_MVC.reg.Test( input )
	End Function
	
	'正则替换
	Function reg_replace( ByRef input, Byref sReplace ,ByRef pattern, ByRef mode)		
		Call setReg( pattern,mode )			
		reg_replace = POP_MVC.reg.Replace( input, sReplace)
	End Function
	
	'''''''''''''''判断函数
	
	'判断字符串是否存在另一字符串中
	Function Exists( input,find )
		Exists = (inStr( input,find ) > 0)
	End Function
	
	Function iExists( input,find )
		iExists = (inStr( LCase(input),LCase(find) ) > 0)
	End Function
	
	'判断某字符串是否以另一字符串开头
	Function StartsWith( ByVal input,ByVal find )
		dim find_len
		if typename(input)<> "String" or typename(find)<> "String" then
			StartsWith = False : Exit Function
		end if
		StartsWith =  (cmp( left(input,len( find )) , find ) = 0)
	End Function
	
	'判断某字符串是否以另一字符串开头，忽略大小写
	Function iStartsWith( ByVal input,ByVal find )
		dim find_len
		if typename(input)<> "String" or typename(find)<> "String" then
			iStartsWith = False : Exit Function
		end if
		iStartsWith =  (casecmp( left(input,len( find )) , find ) = 0)
	End Function
	
	'判断某字符串是否以另一字符串结尾
	Function EndsWith( ByVal input,ByVal find )
		dim find_len
		if typename(input)<> "String" or typename(find)<> "String" then
			EndsWith = False : Exit Function
		end if
		EndsWith =  (cmp( right(input,len( find )) , find ) = 0)
	End Function
	
	
	'判断某字符串是否以另一字符串结尾，忽略大小写
	Function iEndsWith( ByVal input,ByVal find )
		dim find_len
		if typename(input)<> "String" or typename(find)<> "String" then
			iEndsWith = False : Exit Function
		end if
		iEndsWith =  (casecmp( right(input,len( find )) , find ) = 0)
	End Function
	
	'''''''''''''''比较函数
	
	'判断两字符串是否相同
	Function Equal( ByVal str1,ByVal str2 )
		if typename(str1)<> "String" or typename(str2)<> "String" then
			Equal = False : Exit Function
		end if
		Equal =  (cmp( str1,str2 ) = 0)
	End Function
	
	'判断两字符串是否相同，忽略大小写
	Function iEqual( ByVal str1,ByVal str2 )
		if typename(str1)<> "String" or typename(str2)<> "String" then
			iEqual = False : Exit Function
		end if
		iEqual =  (casecmp( str1,str2 ) = 0)
	End Function
	
	'将字符串的首字母转换为大写
	Function UCFirst(ByRef str)
		Dim first
		if str="" Then
			UCFirst = ""
		Else
			first = Mid(str,1,1)
			UCFirst = UCase(first) & Mid(str,2)
		End If		
	End Function
	
	'将字符串的首字母转换为小写
	Function LCFirst(ByRef str)
		Dim first
		if str="" Then
			LCFirst = ""
		Else
			first = Mid(str,1,1)
			LCFirst = LCase(first) & Mid(str,2)
		End If		
	End Function
	

	
	' 二进制安全比较字符串大小
	function cmp( ByRef str1,ByRef str2 )
		cmp = strComp( str1, str2 )
	End Function
	
	' 二进制安全比较字符串大小，忽略大小写
	function casecmp( ByRef str1,ByRef str2 )
		casecmp = cmp( LCase(str1), LCase(str2) )
	End Function
	
	'查找字符串的首次出现，返回字符串的一部分或者 Empty（如果未发现）
	Function str( ByRef input ,ByRef find )
		Dim pos
		pos = inStr(input,find)
		if pos>0 then str = mid( input,pos )
	End Function

	'查找字符串的最后一次出现，返回字符串的一部分或者 Empty（如果未发现）	
	Function rstr( ByRef input ,ByRef find )
		Dim pos
		pos = inStrRev( input,find )
		if pos>0 then rstr = mid( input,pos ) 
	End Function
	

	
	'使用另一个字符串填充字符串为指定长度
	'input: 输入字符串。
	'pad_length: 如果 pad_length 的值是负数，小于或者等于输入字符串的长度，不会发生任何填充。
	'pad_string: 如果填充字符的长度不能被 pad_string 整除，那么 pad_string 可能会被缩短。
	'action: 填充方向，可为left、right、both
	Private function padding(ByVal input,ByRef pad_length,ByVal pad_string,ByRef action)
		dim input_length,padding_length
		input_length = len(input)
		if pad_length <= input_length Then
			padding = input : exit function
		end if
		
		pad_string = CStr(pad_string)
		if pad_string = "" then pad_string = " "		
		padding_length = len( pad_string )
		
		'循环填充
		do until input_length >=  pad_length
			if action = "left" then
				input = pad_string & input				
			else
				input = input & pad_string	
			end if
			
			input_length = input_length + padding_length
			
			'两侧填充时，得实时检测其长度
			if action = "both" and input_length < pad_length then
				input = pad_string & input
				input_length = input_length + padding_length				
			end if
		loop
		
		'填充长了，还得修剪
		if input_length > pad_length then
			if action = "left" then
				input = right( input,pad_length )				
			else
				input = left( input,pad_length )
			end if
		end if
		padding = input
	end Function
	
	'对正则匹配模式进行设置
	Private Sub setReg(ByRef pattern, ByRef mode)
		'忽略大小写
		If iExists(mode,"i") Then 
			POP_MVC.reg.IgnoreCase = true
		Else
			POP_MVC.reg.IgnoreCase = false 
		End If
		
		'全部
		If iExists(mode,"g") Then 
			POP_MVC.reg.Global = true
		Else
			POP_MVC.reg.Global = false 
		End If
		
		'多行
		If iExists(mode,"m") Then 
			POP_MVC.reg.MultiLine = true
		Else
			POP_MVC.reg.MultiLine = false 
		End If
		
		POP_MVC.reg.Pattern = pattern
	End Sub	
	
  '将HTML文本转换为HTML代码
  Public Function HtmlDecode(ByVal str)
    If len(str) > 0 Then
		str = Replace(str, "&" , Chr(38))
		str = Replace(str, """", Chr(34))
		str = Replace(str, "<" , Chr(60))
		str = Replace(str, ">" , Chr(62))
		str = Replace(str, " " , Chr(32))
    End If
    HtmlDecode = str
  End Function
End Class
%>